<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  Application
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Abstract.class.php 21 2012-07-16 21:33:14Z purcaholic $
 * @todo        Error handling
 */


/**
 * Abstract CONTENIDO core class.
 *
 * Contains methods to access to global used variables.
 *
 * Ensures that some global identifier (e. g. $lang, $client, $idart, $idcat)
 * are usable as numeric values. Works also as a wrapper for some globals like,
 * $cfg, $cfgClient, $perm, $auth, $session, ...
 *
 * @package     mpContenidoX
 * @subpackage  Application
 * @author      Murat Purc <murat@purc.de>
 */
abstract class mpContenido_Core_Abstract
{

    /**
     * Returns configuration.
     *
     * @return  array|null
     */
    public static function getConfig()
    {
        return (is_array(self::_getGlobal('cfg'))) ? self::_getGlobal('cfg') : null;
    }


    /**
     * Returns client configuration, either the configuration of a specific client
     * or of all clients.
     *
     * @param   int  $idclient  Id of client
     * @return  array|null
     */
    public static function getClientConfig($idclient=null)
    {
        $cfgClient = self::_getGlobal('cfgClient');
        if (!is_array($cfgClient)) {
            return null;
        }
        if ($idclient !== null) {
            $idclient = self::getClientId($idclient);
            return (isset($cfgClient[$idclient]) && is_array($cfgClient[$idclient])) ? $cfgClient[$idclient] : null;
        } else {
            return $cfgClient;
        }
    }


    /**
     * Returns the language id, ensures that passed id is numeric an greater than 0.
     *
     * @param   int  $idlang  Language id, uses the current language id by default.
     * @return  int  Language id
     * @throws  Exception  If language id is less than 1
     */
    public function getLanguageId($idlang=null)
    {
        return self::_getLanguageId($idlang);
    }


    /**
     * Returns the client id, ensures that passed id is numeric an greater than 0.
     *
     * @param   int  $idclient  Client id, uses the current client id by default.
     * @return  int  Client id
     * @throws  Exception  If client id is less than 1
     */
    public static function getClientId($idclient=null)
    {
        return self::_getClientId($idclient);
    }


    /**
     * Returns the article id, ensures that passed id is numeric an greater than 0.
     *
     * @param   int  $idart  Article id, uses the current article id by default.
     * @return  int  Article id
     * @throws  Exception  If article id is less than 1
     */
    public static function getArticleId($idart=null)
    {
        return self::_getArticleId($idart);
    }


    /**
     * Returns the category id, ensures that passed id is numeric an greater than 0.
     *
     * @param   int  $idcat  Category id, uses the current category id by default.
     * @return  int  Category id
     * @throws  Exception  If category id is less than 1
     */
    public static function getCategoryId($idcat=null)
    {
        return self::_getCategoryId($idcat);
    }


    /**
     * Returns the auth object.
     *
     * @return  Contenido_Auth|null
     */
    public static function getAuthentication()
    {
        return (is_object(self::_getGlobal('auth'))) ? self::_getGlobal('auth') : null;
    }


    /**
     * Returns the perm object.
     *
     * @return  Contenido_Perm|null
     */
    public static function getPermission()
    {
        return (is_object(self::_getGlobal('perm'))) ? self::_getGlobal('perm') : null;
    }


    /**
     * Returns the session object.
     *
     * @return  Contenido_Session|Contenido_Frontend_Session|null  Depends on backend/frontend
     */
    public static function getSession()
    {
        return (is_object(self::_getGlobal('sess'))) ? self::_getGlobal('sess') : null;
    }


    /**
     * Checks if application runs in backend
     *
     * @return bool
     */
     public static function isBackend()
     {
        return (self::_getGlobal('contenido')) ? true : false;
     }


    /**
     * Checks if application runs in backend and editmode
     *
     * @return bool
     */
     public static function isBackendAndEditMode()
     {
        return (self::_getGlobal('contenido') && self::_getGlobal('edit'));
     }


    /**
     * Returns the client id.
     *
     * Type casts passed client id to a integer value or returns current global client id.
     *
     * @param   int  $idclient  The client id to clean and return.
     * @return  Client id
     * @throws  Exception  If client id is less than 1
     */
    protected static function _getClientId($idclient=null)
    {
        $idclient = (int) $idclient;
        if ($idclient < 1) {
            $idclient = (int) self::_getGlobal('client');
        }
        if ($idclient < 1) {
            throw new Exception("Couldn't set client identifier");
        }
        return $idclient;
    }


    /**
     * Returns the language id.
     *
     * Type casts passed language id to a integer value or returns current global language id.
     *
     * @param   int  $idlang  The language id to clean and return.
     * @return  Language id
     * @throws  Exception  If language id is less than 1
     */
    protected static function _getLanguageId($idlang=null)
    {
        $idlang = (int) $idlang;
        if ($idlang < 1) {
            $idlang = (int) self::_getGlobal('lang');
        }
        if ($idlang < 1) {
            throw new Exception("Couldn't set language identifier");
        }
        return $idlang;
    }


    /**
     * Returns the article id.
     *
     * Type casts passed article id to a integer value or returns current global article id.
     *
     * @param   int  $idart  The article id to clean and return.
     * @return  Article id
     * @throws  Exception  If article id is less than 1
     */
    protected static function _getArticleId($idart=null)
    {
        $idart = (int) $idart;
        if ($idart < 1) {
            $idart = (int) self::_getGlobal('idart');
        }
        if ($idart < 1) {
            throw new Exception("Couldn't set article identifier");
        }
        return $idart;
    }


    /**
     * Returns the category id.
     *
     * Type casts passed category id to a integer value or returns current global category id.
     *
     * @param   int  $idcat  The category id to clean and return.
     * @return  Category id
     * @throws  Exception  If category id is less than 1
     */
    protected static function _getCategoryId($idcat=null)
    {
        $idcat = (int) $idcat;
        if ($idcat < 1) {
            $idcat = (int) self::_getGlobal('idcat');
        }
        if ($idcat < 1) {
            throw new Exception("Couldn't set category identifier");
        }
        return $idcat;
    }


    /**
     * Just a wrapper for variables in superglobal $GLOBALS.
     *
     * @param   string  $name  Name of global
     * @param   mixed   $default  Default value to return
     * @return  mixed|null
     */
    protected static function _getGlobal($name, $default=null)
    {
        return (isset($GLOBALS[$name])) ? $GLOBALS[$name] : $default;
    }

}
